/* Copyright (c) 2011-2013 ISP RAS, Moscow. For more information please write to
 
      mermakov@ispras.ru
      svartanov@ispras.ru

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package ru.ispras.coffeemachine.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Utility class for automatic identification of GUI-related methods/classes that need extra
 * instrumentation, mainly GUI event registering.
 * 
 */

public class FrameworkUtil {

	public static enum ListenerType {
		/**
		 * Add <code>EVENT_START</code> on method entry
		 */
		EVENT_STARTER,
		/**
		 * Add <code>EVENT_END</code> on method exit
		 */
		EVENT_FINISHER,
		/**
		 * Add both <code>EVENT_START</code> and <code>EVENT_END</code> in respective positions
		 */
		FULL_EVENT;

		public boolean isFinisher() {

			if (!equals(EVENT_STARTER)) {
				return true;
			}

			return false;
		}

		public boolean isStarter() {

			if (!equals(EVENT_FINISHER)) {
				return true;
			}

			return false;
		}
	}

	/**
	 * Event listener class that specifies method name and type
	 * 
	 */
	public static class EventListener {
		public String name;
		public ListenerType type;

		public EventListener(String name, ListenerType type) {

			this.name = name;
			this.type = type;
		}
	}

	public static Map<String, List<EventListener>> eventListeners = new HashMap<String, List<EventListener>>();

	private static void addAndroidListeners() {

		eventListeners.put(	"android.view.View$OnClickListener",
							Arrays.asList(new EventListener("onClick", ListenerType.FULL_EVENT)));

		eventListeners.put(	"android.view.View$OnLongClickListener",
							Arrays.asList(new EventListener("onLongClick", ListenerType.FULL_EVENT)));
		eventListeners.put(	"android.view.View$OnTouchListener",
							Arrays.asList(new EventListener("onTouch", ListenerType.FULL_EVENT)));
		eventListeners.put(	"android.widget.AdapterView$OnItemSelectedListener",
							Arrays.asList(new EventListener("onItemSelected",
															ListenerType.FULL_EVENT)));
	}

	private static void addSwingListeners() {

		eventListeners.put(	"java.awt.event.ActionListener",
							Arrays.asList(new EventListener("actionPerformed",
															ListenerType.FULL_EVENT)));
		eventListeners.put(	"java.awt.event.MouseListener",
							Arrays.asList(	new EventListener(	"mouseClicked",
																ListenerType.FULL_EVENT),
											new EventListener(	"mousePressed",
																ListenerType.EVENT_STARTER),
											new EventListener(	"mouseReleased",
																ListenerType.EVENT_FINISHER)));
	}

	static {
		addAndroidListeners();
		addSwingListeners();
	}

	/**
	 * Checks whether registered listeners cover target class.
	 * 
	 * @param className target class name
	 * @return <code>true</code> if there is at least one listener for target class,
	 *         <code>false</code> otherwise
	 */
	public static boolean isListenerClass(String className) {

		return eventListeners.keySet().contains(className);
	}

	/**
	 * Returns listener type for target method.
	 * 
	 * @param className target class name
	 * @param methodName target method name
	 * @return <code>null</code> if there is no listener for target method, listener type otherwise
	 */
	public static ListenerType getListenerType(String className, String methodName) {

		if ((className == null) || (methodName == null)) {
			return null;
		}
		List<EventListener> listeners = eventListeners.get(className);
		if (listeners == null) {
			return null;
		}
		for (EventListener listener : listeners) {
			if (methodName.contains(listener.name)) {
				return listener.type;
			}
		}
		return null;
	}

	/*
	 * public static class WidgetDataAccessor { public String name; public Class<?> type;
	 * 
	 * public WidgetDataAccessor(String name, Class<?> type) {
	 * 
	 * this.name = name; this.type = type; } }
	 * 
	 * public static Map<String, List<WidgetDataAccessor>> widgetDataAccessors = new HashMap<String,
	 * List<WidgetDataAccessor>>();
	 * 
	 * private static void addSwingDataAccessors() {
	 * 
	 * widgetDataAccessors.put("javax/swing/JTextField", Arrays.asList(new
	 * WidgetDataAccessor("getText", String.class))); }
	 * 
	 * private static void addAndroidDataAccessors() {
	 * 
	 * widgetDataAccessors.put("android.widget.EditText", Arrays.asList(new
	 * WidgetDataAccessor("getText", String.class))); }
	 * 
	 * static { //addSwingDataAccessors(); //addAndroidDataAccessors(); }
	 * 
	 * public static WidgetDataAccessor getWidgetDataAccessor(String className, String methodName) {
	 * className = className.replaceAll("/", "."); List<WidgetDataAccessor> accessors =
	 * widgetDataAccessors.get(className); if (accessors == null) {
	 * //System.out.println("no accessors for " + className + ":" + methodName); return null; } for
	 * (WidgetDataAccessor accessor : accessors) { if (accessor.name.equals(methodName)) {
	 * //System.out.println("found accessors for " + className + ":" + methodName); return accessor;
	 * } } //System.out.println("no accessors for " + className + ":" + methodName); return null; }
	 * 
	 * public static Object getWidgetData(WidgetDataAccessor accessor, String elementId) { if
	 * (accessor.type == String.class) { return new StringElement(elementId + "_data", null, true,
	 * null); } else { return new RefElement(null, null, false, 0); } }
	 */

}
